<template>
  <div class="root">
    <el-table
      :data="tableData"
      border
      row-key="id"
      style="width: 100%"
      :fit='true'
      :default-sort="{prop: 'id', order: 'descending'}"
    >
      <el-table-column sortable show-overflow-tooltip v-for="(item, index) in tableHeader"
                       :key="tableHeader[index]"
                       :prop="dropCol[index].prop"
                       :label="item.label"
                       align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-button size="small" type="danger" icon=Edit @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="primary" icon=Delete @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  name: "test",
  data() {
    return {
      tableHeader: [
        {
          label: 'ID',
          prop: 'id'
        },
        {
          label: '服务器类型',
          prop: 'type'
        },
        {
          label: '业务类型',
          prop: 'businessType'
        },
        {
          label: '所属部门',
          prop: 'department'
        },
        {
          label: 'IP地址',
          prop: 'ipaddress'
        },
        {
          label: '规格',
          prop: 'standard'
        },
        {
          label: '用途',
          prop: 'use'
        },
        {
          label: '更新时间',
          prop: 'date'
        },
      ],
      dropCol: [
        {
          label: 'ID',
          prop: 'id'
        },
        {
          label: '服务器类型',
          prop: 'type'
        },
        {
          label: '业务类型',
          prop: 'businessType'
        },
        {
          label: '所属部门',
          prop: 'department'
        },
        {
          label: 'IP地址',
          prop: 'ipaddress'
        },
        {
          label: '规格',
          prop: 'standard'
        },
        {
          label: '用途',
          prop: 'use'
        },
        {
          label: '更新时间',
          prop: 'date'
        },
      ],
      tableData: []
    }
  },
  methods: {
    formatter(row, column) {
      return row.address;
    },
    handleEdit (index, row) {
      console.log(index, row)
    },
    handleDelete (index, row) {
      console.log(index, row.username)
    },
    async getData() {
      const serverList = await this.$api.serverList()
      // console.log(serverList)
      this.tableData = serverList.data
      // for (var i = 0; i < serverList.data.length; i++) {
      //   // console.log(serverList[i])
      //   this.tableData.push(serverList[i])
      // }
    },
    //行拖拽
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const _this = this
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          console.log("拖动了行","当前序号: "+newIndex)
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.tableData.splice(newIndex, 0, currRow)
        }
      })
    },
    //列拖拽
    columnDrop() {
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: evt => {
          console.log("拖动了列")
          const oldItem = this.dropCol[evt.oldIndex]
          this.dropCol.splice(evt.oldIndex, 1)
          this.dropCol.splice(evt.newIndex, 0, oldItem)
        }
      })
    }
  },
  mounted() {
    this.getData()
    this.rowDrop()
    this.columnDrop()
  }
}
</script>

<style scoped>
.root {
  margin: 10px 10px 0px 10px;
}

/deep/.el-table .cell{
  display: flex;
  justify-content: center;
}
</style>
